package com.zzg.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.google.code.kaptcha.Constants;
import com.zzg.base.response.BaseResponse;
import com.zzg.util.result.ResultUtil;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "jenny页面入口",description = "jenny页面入口管理")
@Controller
@Slf4j
public class IndexController {
	/***
     * 首页
     * @return
     */
    @ApiOperation(value = "首页", notes = "首页")
    @GetMapping(value = {"/","/common/index", "/index"})
    public String index(){
        return "common/index";
    }

    /***
     * 登录
     * @return
     */
    @ApiOperation(value = "登录", notes = "登录")
    @GetMapping("/login")
    public String login() {
        return "common/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public BaseResponse login(HttpServletRequest request,String username,String password,String verification,
                            @RequestParam(name = "rememberme",defaultValue = "false") Boolean rememberme){
        log.info("[进入登录方法....]");
        //判断验证码
        String rightCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        if (StringUtils.isNotBlank(verification) && StringUtils.isNotBlank(rightCode) && verification.equals(rightCode)) {
            log.info("[验证码通过]");
        } else {
           return ResultUtil.error("验证码错误！");
        }
        UsernamePasswordToken token=new UsernamePasswordToken(username,password,rememberme);
        try {
            Subject subject= SecurityUtils.getSubject();
            //登录验证
            subject.login(token);
        }catch (Exception e){
            token.clear();
            log.info("[登录内部错误！请联系管理员检查！]-[{}]",DateUtil.date());
            return ResultUtil.error(e.getMessage());
        }
        log.info("[登录成功]-[{}]",DateUtil.date());
        return ResultUtil.success("登录成功！");
    }

    /**
     * 退出登录
     * @return
     */
    @ApiOperation(value = "退出登录", notes = "退出登录")
    @GetMapping("/logout")
    public ModelAndView logout() {
        // http://www.oschina.net/question/99751_91561  此处有坑，这里其实可用使用shiro自带的退出，不用你实现任何东西
        log.info("[退出登录成功]-[{}]",DateUtil.date());
        return ResultUtil.redirect("login");
    }

    /***
     * hope-plus
     * @param model
     * @return
     */
    @ApiOperation(value = "hope-plus", notes = "hope-plus")
    @RequestMapping("/hope-plus")
    public ModelAndView index_v1(Model model){
        return ResultUtil.view("common/hope-plus");
    }
}
